package com.chaodev.base.service.impl;

import com.chaodev.base.dao.FileDao;
import com.chaodev.base.service.IFileService;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class FileServiceImpl implements IFileService {
    @Autowired
    FileDao fileDao;
    private  List<File> importFiles;
    private List<Map<String,Object>> ftpFiles;
    private FTPClient ftp;
    @Override
    public String remUploadFile(int daysBefore,String basePath) {
        File file=new File(basePath);
        long remSize=0;
        importFiles=new ArrayList<>();
        this.findFile(file,daysBefore);
        if(!importFiles.isEmpty()){
            long nowTime=System.currentTimeMillis();
            //遍历文件，并删除n天以前的
            for(File file1:importFiles){
                    remSize+=file1.length();
                    file1.delete();
            }
        }
        //删除完毕后释放内存
        importFiles=null;
        //如果大于1MB显示MB，否则显示KB
        if(remSize>=1048576L){
            long formatSize=remSize/1048576;
            return formatSize+"MB";
        }
        else{
            long formatSize=remSize/1024;
            return formatSize+"KB";
        }
    }

    @Override
    public String remFtpUploadFile(FTPClient ftpClient,int daysBefore,String basePath) throws IOException {
        long remSize=0;
        // 连接FTP服务器
        ftp =ftpClient;
        ftp.changeWorkingDirectory(basePath);
        findFtpFile(basePath,daysBefore);
        for(Map<String,Object> map:ftpFiles){
            remSize+=(long)map.get("size");
            ftp.deleteFile(map.get("path").toString());
        }
        ftpFiles=null;
        if(remSize>=1048576L){
            long formatSize=remSize/1048576;
            return formatSize+"MB";
        }
        else{
            long formatSize=remSize/1024;
            return formatSize+"KB";
        }
    }

    @Override
    public String countUploadFile(int daysBefore, String basePath) {
        File file=new File(basePath);
        long remSize=0;
        importFiles=new ArrayList<>();
        this.findFile(file,daysBefore);
        if(!importFiles.isEmpty()){
            long nowTime=System.currentTimeMillis();
            //遍历文件，并删除n天以前的
            for(File file1:importFiles){
                remSize+=file1.length();
            }
        }
        //删除完毕后释放内存
        importFiles=null;
        //如果大于1MB显示MB，否则显示KB
        if(remSize>=1048576L){
            long formatSize=remSize/1048576;
            return formatSize+"MB";
        }
        else{
            long formatSize=remSize/1024;
            return formatSize+"KB";
        }
    }

    @Override
    public String countFtpUploadFile(FTPClient ftpClient, int daysBefore, String basePath) throws IOException {
        long remSize=0;
        // 连接FTP服务器
        ftp =ftpClient;
        ftp.changeWorkingDirectory(basePath);
        findFtpFile(basePath,daysBefore);
        for(Map<String,Object> map:ftpFiles){
            remSize+=(long)map.get("size");
        }
        ftpFiles=null;
        if(remSize>=1048576L){
            long formatSize=remSize/1048576;
            return formatSize+"MB";
        }
        else{
            long formatSize=remSize/1024;
            return formatSize+"KB";
        }
    }

    @Override
    public List<String> getHistoryImportFile(Map<String, Object> map) {
        int daysBefore=(int)map.get("daysBefore");
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH,-daysBefore);
        map.put("endDate",simpleDateFormat.format(cal.getTime()));
        return fileDao.getHistoryImportFile(map);
    }

    @Override
    public Long getHistoryImportSize(Map<String, Object> map) {
        int daysBefore=(int)map.get("daysBefore");
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH,-daysBefore);
        map.put("endDate",simpleDateFormat.format(cal.getTime()));
        return fileDao.getHistoryImportSize(map);
    }

    @Override
    public int updateImportInfo(Map<String, Object> map) {
        int daysBefore=(int)map.get("daysBefore");
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH,-daysBefore);
        map.put("endDate",simpleDateFormat.format(cal.getTime()));
        return fileDao.updateImportInfo(map);
    }

    private  void findFile(File file,int daysBefore){
        File[] files=file.listFiles();
        long nowTime=System.currentTimeMillis();
        if(files!=null&&files.length>0){
            for(File file1:files){
                if(file1.isDirectory()){
                    findFile(file1,daysBefore);
                }
                else{
                    long fileDate=file1.lastModified();
                    if(nowTime-fileDate> (long) daysBefore *86400000L) {
                        importFiles.add(file1);
                    }
                }
            }
        }
    }
    private  void findFtpFile(String pathName,int daysBefore) throws IOException {
        this.ftp.changeWorkingDirectory(pathName);
        FTPFile[] files=this.ftp.listFiles();
        long nowTime=System.currentTimeMillis();
        for(FTPFile file:files){
            if(file.isFile()){
                //超过固定天数前的文件再添加到待删除列表中
                long uploadTime=file.getTimestamp().getTimeInMillis();
                if(nowTime-uploadTime>(long) daysBefore *86400000L){
                    Map<String,Object> map=new HashMap<>();
                    map.put("path",pathName+file.getName());
                    map.put("size",file.getSize());
                    ftpFiles.add(map);
                }
            }
            else{
                findFtpFile(pathName+file.getName()+"/",daysBefore);
            }
        }
    }
}
